﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;

namespace System.Web.Mvc.Html
{
    public static class DialogExtension
    {
        public static string Dialog(this HtmlHelper htmlHelper,
            string dialogName, string dialogTitle,
            string partialViewName, string controller, string buttonTitle,
            DialogButton[] buttons, string dialogNameButton)
        {

            StringBuilder sb = new StringBuilder();
            sb.Append("<script type=\"text/javascript\">");
            sb.Append("$.ajaxSetup ({ cache: false });");
            sb.Append("$(function(){");
            sb.Append("$('#" + dialogNameButton + "').click(function(){");
            sb.Append("$('#" + dialogName + "').load(\"" + controller + "\");");
            sb.Append("$('#" + dialogName + "').dialog('open');");
            sb.Append("return false;");
            sb.Append("});");
            sb.Append("$('#" + dialogName + "').dialog({");
            sb.Append("autoOpen: false,");
            sb.Append("width: 600,");
            sb.Append("modal: true, ");
            sb.Append("title: \"" + dialogTitle + "\",");
            sb.Append("buttons: {");
            int cButton = 0;
            int cAttribute = 0;
            foreach (var button in buttons)
            {
                cButton++;
                sb.Append("\"" + button.buttonName + "\": function() {");
                if (button.cancel)
                {
                    sb.Append("$(this).dialog(\"close\");");
                }
                else
                {
                    foreach (var attribute in button.attributes)
                    {
                        sb.Append("var " + attribute + " = " + "$(\"#" + attribute + "\").val();");
                    }
                   
                    cAttribute = 0;
                    
                    sb.Append("$.post(\"" + button.controllerAction + "\", {");
                    foreach (var attribute in button.attributes)
                    {
                        cAttribute++;
                        sb.Append("\"" + attribute + "\": " + attribute);
                        if (cAttribute != button.attributes.Length)
                        {
                            sb.Append(",");
                        }    
                    }
                    sb.Append("}, function(result) {");
                    
                    sb.Append("$(\"#" + partialViewName  + " form\").append($(result));});");
                    sb.Append("$(this).dialog(\"close\");");
                    if (button.contentUpdate)
                    {
                        sb.Append("$('#" + button.contentId + "').load(\"" + button.ContentUpdateAction + "\");");                        
                    }
                }
                sb.Append("}");
                if (cButton != buttons.Length)
                {
                    sb.Append(",");
                }
            }
            sb.Append("}");
            sb.Append("});");
            sb.Append("});");
            sb.Append("</script> ");
            sb.Append("<div id=\"" + dialogName + "\"></div>");
            sb.Append("<a href=\"#\" id=\"" + dialogNameButton + "\" class=\"fg-button fg-button-icon-left ui-state-default ui-corner-all\"><span class=\"ui-icon ui-icon-plusthick\"></span>" + buttonTitle + "</a>");
            return sb.ToString();
        }

        public class DialogButton
        {
            private string _buttonName;
            private string[] _attributes;
            private bool _cancel;
            private string _controllerAction;
            private bool _contentUpdate;
            private string _ContentUpdateAction;
            private string _Contentid;
            private bool _modal;

            public bool modal
            {
                get { return this._modal; }
                set { this._modal = value; }
            }

            public string contentId
            {
                get { return this._Contentid; }
                set { this._Contentid = value; }
            }

            public bool contentUpdate
            {
                get { return this._contentUpdate; }
                set { this._contentUpdate = value; }
            }

            public string ContentUpdateAction
            {
                get { return this._ContentUpdateAction; }
                set { this._ContentUpdateAction = value; }
            }

            public string controllerAction
            {
                get { return this._controllerAction; }
                set { this._controllerAction = value; }
            }

            public string buttonName
            {
                get { return this._buttonName; }
                set { this._buttonName = value; }
            }

            public string[] attributes
            {
                get { return this._attributes; }
                set { this._attributes = value; }
            }

            public bool cancel
            {
                get { return this._cancel; }
                set { this._cancel = value; }
            }
        }
    }
}
